use std::convert::From;

#[derive(Debug)]
struct Number {
    value: i32,
}

impl From<i32> for Number {
    fn from(item: i32) -> Self{
        Number { value: item }
    }
}

fn from_into(){
    { //from 
        let num = Number::from(12);
        println!("My number is  {:?}, {}", num, num.value);
    }

    { //into
        let int = 5;
        let num: Number = int.into();
        println!("My number is {:?}", num);
    }
}


use std::convert::TryFrom;
//use std::convert::TryInto;

#[derive(Debug, PartialEq)]
struct EvenNumber(i32);

impl TryFrom<i32> for EvenNumber {
    type Error = ();
    fn try_from(value: i32) -> Result<Self, Self::Error> {
        if value %2 == 0 {
            Ok(EvenNumber(value))
        }
        else {
            Err( () )
        }
    }
}


fn try_from_into(){
    assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8)));
    assert_eq!(EvenNumber::try_from(5), Err(()));

    let result: Result<EvenNumber, ()> = 8i32.try_into();
    assert_eq!(result, Ok(EvenNumber(8)));

    let result: Result<EvenNumber, ()> = 5i32.try_into();
    assert_eq!(result, Err( () ));
}


use std::fmt;
struct Circle {
    radius: i32
}

impl fmt::Display for Circle {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 
        write!(f, "Circle of radius {}", self.radius)
    }
}


fn to_string(){
    let circle = Circle { radius: 6 };
    println!("{}", circle.to_string());
}

fn parse_string(){
    let parsed: i32 = "5".parse().unwrap();
    let turbo_parsed = "10".parse::<i32>().unwrap();

    let sum = parsed + turbo_parsed;
    println!("Sum : {:?}", sum);

}

pub fn main(){
    from_into();
    try_from_into();
    to_string();
    parse_string();
}
